package com.ctshk.rpc.payment.factory;

import cn.hutool.core.collection.CollectionUtil;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.utils.Assert;
import com.ctshk.rpc.payment.dto.RefundDetailsDTO;
import com.ctshk.rpc.payment.dto.rep.alipay.AliPayRefundQueryDTO;
import com.ctshk.rpc.payment.dto.rep.unionpay.UnionPayQueryTransDTO;
import com.ctshk.rpc.payment.dto.rep.wechat.WxPayRefundDetailDTO;
import com.ctshk.rpc.payment.enums.RefundStatus;

import java.math.BigDecimal;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.rpc.payment.factory
 * @Author: hef
 * @CreateTime: 2021-05-08 15:07
 * @Description: 退款详情简单工厂
 */
public class RefundDetailsDTOSimpleFactory {
    /**
     * 微信退款详情转换
     *
     * @param refundDetailDTO
     * @return
     */
    public static RefundDetailsDTO produce(WxPayRefundDetailDTO refundDetailDTO) {
        if (refundDetailDTO == null ||
                CollectionUtil.isEmpty(refundDetailDTO.getList())) return null;
        WxPayRefundDetailDTO.RefundItem refundItem = refundDetailDTO.getList().get(0);
        RefundDetailsDTO refundDetails = new RefundDetailsDTO();
        refundDetails.setRefundNumber(refundItem.getRefundNo());
        refundDetails.setRefundAmount(BigDecimal.valueOf(refundDetailDTO.getRefundFee()).divide(BigDecimal.valueOf(100)));
        RefundStatus status = null;
        switch (refundItem.getRefundStatus()) {
            case "SUCCESS":
                status = RefundStatus.SUCCESS;
                break;
            case "PROCESSING":
                status = RefundStatus.PROCESSING;
                break;
            default:
                status = RefundStatus.FAILURE;
                break;
        }
        refundDetails.setRefundStatus(status);
        refundDetails.setRefundTime(refundItem.getRefundSuccessTime());
        refundDetails.setCurrency(refundDetailDTO.getFeeType());
        return refundDetails;
    }

    public static RefundDetailsDTO produce(AliPayRefundQueryDTO dto) {
        if (dto == null || !dto.isSuccess()) return null;
        AliPayRefundQueryDTO.ResponseInfoItem item = dto.getResponse().getAlipay();
        if (!"SUCCESS".equals(item.getResponseCode())) {
            Assert.isTrue(!"NOT_FOUND".equals(item.getResponseCode()), SystemError.PAY_22013);
            Assert.isTrue(false, SystemError.PAY_22014, item.getResponseCode());
        }
        RefundDetailsDTO refundDetailsDTO = new RefundDetailsDTO();
        refundDetailsDTO.setOrderNumber(item.getOutTradeNo());
        refundDetailsDTO.setRefundNumber(item.getOutReturnNo());
        refundDetailsDTO.setRefundAmount(item.getRefundForeignAmount());
        RefundStatus status = null;
        switch (item.getRefundResultCode()) {
            //退款成功。
            case "SUCCESS":
                status = RefundStatus.SUCCESS;
                break;
            //支付宝系统正在处理退款。
            case "PROCESSING":
                status = RefundStatus.PROCESSING;
                break;
            default:
                status = RefundStatus.FAILURE;
                break;
        }
        refundDetailsDTO.setRefundStatus(status);
        refundDetailsDTO.setRefundTime(item.getGmtFinished());
        refundDetailsDTO.setCurrency(item.getCurrency());
        return refundDetailsDTO;
    }

    public static RefundDetailsDTO produce(UnionPayQueryTransDTO queryTrans) {
        Assert.isTrue("00".equals(queryTrans.getRespCode()), SystemError.PAY_22014, queryTrans.getRespCode());
        RefundDetailsDTO dto = new RefundDetailsDTO();
        dto.setRefundNumber(queryTrans.getOrderId());
        dto.setRefundAmount(BigDecimal.valueOf(Long.valueOf(queryTrans.getTxnAmt()))
                .divide(BigDecimal.valueOf(100)));
        String origRespCode = queryTrans.getOrigRespCode();
        RefundStatus status = null;
        if ("00".equals(origRespCode)) {
            status = RefundStatus.SUCCESS;
        } else if ("03".equals(origRespCode) || "04".equals(origRespCode) || "05".equals(origRespCode)) {
            status = RefundStatus.PROCESSING;
        } else {
            status = RefundStatus.FAILURE;
        }
        dto.setRefundStatus(status);
//        dto.setRefundTime(queryTrans.getTraceTime());
        dto.setCurrency(queryTrans.getCurrencyCode());
        return dto;
    }
}
